<?php
ArrowController::importModel("org.arrowplatform.controls.forms.ArrowFormField");
/**
 * Text element
 *
 */
class ArrowFormWyswig extends ArrowFormField{
	
	protected $properties = array(
		"length" => false,
		"style" => '',
		"toolbar" => 'default',
		"preview_url" => false
	
	);
	
	private $htmlId;
	
	public function configure(){
		parent::configure();
		$this->addProperties(array(
			"style_file" => 'default',
			"toolbar" => 'default',
		));
		
	}
	
	public function init(){
		static $rand = null;
		if($rand  == null)
			$rand = rand( 1,100000);
		
		parent::init();
		$res = ArrowController::$activeProject->getResources();
		$this->registerJsFile( $res->getResource("/js/ckeditor/ckeditor.js")->getRelativePath());
		$this->registerJsFile( $res->getResource("/utils/wyswig/conf.js")->getRelativePath()."?".$rand);
		
		
		
		if( $this->getExternalProperty("id") == false )
			$this->htmlId = self::CTRL_NAMESPACE."-wyswig-".$this->getProperty("name");
		else
			$this->htmlId = $this->getExternalProperty("id");

			
			
		$data = array( 
			"toolbar" => $this->getProperty("toolbar"),
			"style_file" => $res->getResource("/utils/wyswig/".$this->getProperty("style_file").".css")->getRelativePath()."?".rand(1,100000),
			"templates" => $res->getResource("/utils/wyswig/templates.js")->getRelativePath()."?".rand(1,100000),
			"path" => $res->getResource("/js/ckeditor")->getRelativePath()."/"
		);
		$this->registerJs(get_class($this), dirname(__FILE__)."/ArrowFormWyswig.js" , "#".$this->htmlId, $data );
	}
	
	public function validate( $proposedValue ){
		return true;
	}
	
	public function generateOutput(){
		$attribuites = array();
		$attribuites["name"] = $this->getHtmlName();
		$attribuites["id"] = $this->htmlId;
		$attribuites["class"] = self::CTRL_NAMESPACE."-wyswig";
		
		$str = "";
		$str.= '<textarea '.$this->genTagsFromArray($attribuites).' '.$this->addExternal().' >';
		$str.= $this->getValue();
		$str.= "</textarea>";
		
		return $str;
	}

}
?>